package cn.fjc920.siaa.activitys

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import cn.fjc920.siaa.R
import cn.fjc920.siaa.activityutils.BaseActivity
import com.azhon.appupdate.listener.OnButtonClickListener
import com.azhon.appupdate.listener.OnDownloadListenerAdapter
import com.azhon.appupdate.manager.DownloadManager
import kotlinx.android.synthetic.main.activity_about.*

class AboutActivity : BaseActivity() {

    private lateinit var context: Context
    private val url = "http://124.221.61.202/Application/gszs.apk"
    private val apkName = "工时助手.apk"
    private lateinit var manager: DownloadManager


    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_about)

        init()
        initClick()
    }

    private fun init() {

        context = this
        setSupportActionBar(findViewById(R.id.about_toolbar))
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
    }

    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
    @SuppressLint("InflateParams")
    private fun initClick() {

        about_toolbar.setNavigationOnClickListener {
            finish()
        }
        btn_update.setOnClickListener {
            //判断是否获取了直接拨打电话权限，如果获取了则直接拨打电话，否则向用户申请授权
            if (ContextCompat.checkSelfPermission(
                    context,
                    Manifest.permission.POST_NOTIFICATIONS
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                ActivityCompat.requestPermissions(
                    this,
                    arrayOf(Manifest.permission.POST_NOTIFICATIONS), 1
                )
            } else {
                startUpdate()
            }
        }
        tv_update.setOnClickListener {
            UpdateActivity.actionStart(this)
        }
        tv_opinion.setOnClickListener {
            OpinionActivity.actionStart(this)
        }
        tv_share.setOnClickListener {
            ShareActivity.actionStart(this)
        }
    }

    private fun startUpdate() {
        manager = DownloadManager.Builder(this).run {
            apkUrl(url)
            apkName(apkName)
            smallIcon(R.mipmap.ic_launcher)
            showNewerToast(true)
            apkVersionCode(7)
            apkVersionName("0.0.7.0308")
            apkSize("4.1MB")
            apkDescription("1、修复app版本更新功能。\n 2、添加桌面快捷方式。\n 3、修复已知bug")
//            apkMD5("DC501F04BBAA458C9DC33008EFED5E7F")

            enableLog(true)
//            httpManager()
            jumpInstallPage(true)
//            dialogImage(R.drawable.ic_dialog)
//            dialogButtonColor(Color.parseColor("#E743DA"))
//            dialogProgressBarColor(Color.parseColor("#E743DA"))
            dialogButtonTextColor(Color.WHITE)
            showNotification(true)
            showBgdToast(false)
            forcedUpgrade(false)
            onButtonClickListener(object : OnButtonClickListener {
                override fun onButtonClick(id: Int) {
                    when (id) {
                        0 -> Toast.makeText(context, "更新包后台下载中...", Toast.LENGTH_SHORT)
                            .show()
                    }
                }
            })
            build()
        }
        manager.download()
    }

    //处理授权或拒绝授权时的回调
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        when (requestCode) {
            1 -> {
                //如果授权了就打电话
                if (grantResults.isNotEmpty() &&
                    grantResults[0] == PackageManager.PERMISSION_GRANTED
                ) {
                    startUpdate()
                } else {
                    //如果拒绝授权就提示用户
                    Toast.makeText(this, "未授予通知权限", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    companion object {
        fun actionStart(context: Context) {
            val intent = Intent(context, AboutActivity::class.java)
            context.startActivity(intent)
        }
    }
}